package com.appleyk.spring.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * <p>越努力，越幸运</p>
 *
 * @author appleyk
 * @version V.0.1.1
 * @blob https://blog.csdn.net/appleyk
 * @date created on  8:16 下午 2021/1/4
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.appleyk.spring.security.controller"})
public class WebConfig implements WebMvcConfigurer {

    // 配置视图解析器
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        // 设置前缀
        viewResolver.setPrefix("WEB-INF/views/");
        // 设置后缀
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 根目录直接跳转到这个 login-view视图，当然这个视图是不存在的，我们要再次映射下
        registry.addViewController("/").setViewName("redirect:/login-view");
        // 将login-view视图映射到login页面上
        registry.addViewController("/login-view").setViewName("login");
    }
}
